package com.htbb.service.personalInsure.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.htbb.common.base.PropertiesConfig;
import com.htbb.order.entity.BsOrderIsu;
import com.htbb.order.entity.TcClaim;
import com.htbb.order.enums.IsuStatus;
import com.htbb.order.interfaces.BsOrderIsuService;
import com.htbb.order.interfaces.TcClaimService;
import com.htbb.order.interfaces.TcIsuService;
import com.htbb.order.interfaces.TcOrderService;
import com.htbb.service.personalInsure.IhtbbService;

@Service
@Transactional
public class IhtbbServiceImpl implements IhtbbService {

	private static final Long END = 4070880000000l;
	
	@Autowired
	private TcOrderService tcOrderService;
	@Autowired
	private TcIsuService tcIsuService;
	@Autowired
	private TcClaimService tcClaimService;

	@Autowired
	private BsOrderIsuService bsOrderIsuService;
	
	@Override
	public boolean updateClaimAndAcctIsu(IsuStatus status, String isuAid, long claimAid, String aisleCode,
			String claimMsg, String claimTime, String url1, String url2, String url3) {
		//更新保单表状态
		tcIsuService.updateIsuStatus(isuAid, IsuStatus.UNKNOWN, status);
		
		//更新理赔表状态
		TcClaim tcClaim = new TcClaim();
		tcClaim.setIsClaim(status.getValue());
		if(status.getValue()==IsuStatus.NO_CLAIM.getValue()){
			tcClaim.setSteps("理赔失败");
		}else if(status.getValue()==IsuStatus.FINISH_FOR_CLAIM.getValue()){
			tcClaim.setSteps("已结案");
		}
		tcClaim.setClaimId(String.valueOf(claimAid));
		tcClaim.setIsuId(isuAid);
		tcClaim.setClaimId(String.valueOf(claimAid));
		tcClaim.setRemark(claimMsg);
		tcClaim.setModifyUser("htbb-interface");
		tcClaim.setModifyDate(new Date());
		
		if(!StringUtils.isEmpty(url1)){
			url1 = url1+ "|" + url2 + "|" + url3;
		}
		tcClaim.setForm2(url1);
		
		tcClaimService.updateNotNull(tcClaim);
		
		return true;
	}


	@Override
	public long insertBsOrderIsu(String isuName, String isuSsn, String sendNum, Date startDate, String address) {
		long aid = PropertiesConfig.iDCreator.nextId();
		Date endDate = new Date(END);
		
		BsOrderIsu bsOrderIsu = new BsOrderIsu();
		bsOrderIsu.setToiAid(String.valueOf(aid));
		bsOrderIsu.setIsuName(isuName);
		bsOrderIsu.setIsuSsn(isuSsn);
		bsOrderIsu.setSendNum(sendNum);
		bsOrderIsu.setStartDate(startDate);
		bsOrderIsu.setEndDate(endDate);
		bsOrderIsu.setSendAddress(address);
		bsOrderIsu.setCreateUser("htbb-interface");
		bsOrderIsu.setModifyUser("htbb-interface");
		bsOrderIsu.setCreateDate(new Date());
		bsOrderIsu.setModifyDate(new Date());
		
		bsOrderIsuService.save(bsOrderIsu);
		return aid;
	}

	@Override
	public BsOrderIsu getBsOrderIsu(String orderId, String isuSsn, String sendNum) {
		BsOrderIsu bsOrderIsu = new BsOrderIsu();
		bsOrderIsu.setToiAid(orderId);
		bsOrderIsu.setIsuSsn(isuSsn);
		bsOrderIsu.setSendNum(sendNum);
		return bsOrderIsuService.selectByOther(bsOrderIsu);
	}

	@Override
	public void updateBsOrderIsu(Long toiAid, Date endDate, int isu_amt) {
		BsOrderIsu bsOrderIsu = new BsOrderIsu();
		bsOrderIsu.setToiAid(String.valueOf(toiAid));
		bsOrderIsu.setEndDate(endDate);
		bsOrderIsu.setIsuAmt(isu_amt);
		bsOrderIsuService.updateNotNull(bsOrderIsu);
	}

	
}
